﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using David.Framework.Core.Enums;
using David.Framework.Core.Security;
using David.WebSite.Data.DataEF;

namespace David.WebSite.Data
{
    /// <summary>
    /// 管理员服务
    /// </summary>
    public class AdminsDataService
    {
        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public static Admin LoginValidation(string userName, string passWord)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                return null;
            }
            using (var db = new DavidDBEntities())
            {
                var adminsInfo = db.Admins.FirstOrDefault(c => c.UserName == userName && c.PassWord == passWord && c.IsDelete == false);
                return adminsInfo;
            }
        }
        /// <summary>
        /// 获取管理账号信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static Admin GetUseInfoByUserId(int userId)
        {
            using (var db = new DavidDBEntities())
            {
                var adminsInfo = db.Admins.FirstOrDefault(c => c.UserId == userId && c.IsDelete == false);
                return adminsInfo;
            }
        }
        /// <summary>
        /// 更新管理信息
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="loginIp"></param>
        public static void UpdateLoginInfo(int userId, string loginIp)
        {
            using (var db = new DavidDBEntities())
            {
                var adminsInfo = db.Admins.FirstOrDefault(c => c.IsDelete == false && c.UserId == userId);
                if (adminsInfo != null)
                {
                    adminsInfo.LastLoginIp = loginIp;
                    adminsInfo.LastLoginTime = DateTime.Now;
                    db.SaveChanges();
                }
            }
        }
        /// <summary>
        /// 添加管理账户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public static bool AddAdmin(string userName, string passWord)
        {
            using (var db = new DavidDBEntities())
            {
                var adminInfo = new Admin()
                {
                    CreateTime = DateTime.Now,
                    IsDelete = false,
                    IsEnable = true,
                    LastLoginIp = null,
                    LastLoginTime = null,
                    PassWord = Encryption.SHA256Encrypt(Encryption.MD5(passWord)),
                    UserName = userName,
                    UserRole = (int)AdminRole.Admin
                };
                db.Admins.Add(adminInfo);
                return db.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 获取管理列表
        /// </summary>
        /// <returns></returns>
        public static List<Admin> GetUseInfoList()
        {
            using (var db = new DavidDBEntities())
            {
                var info =
                    db.Admins.Where(c => c.UserRole != (int)AdminRole.System && c.IsDelete == false)
                        .OrderByDescending(c => c.UserId)
                        .ToList();
                return info;
            }
        }
        /// <summary>
        /// 验证旧密码
        /// </summary>
        /// <param name="oldPass"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool CheckOldPass(string oldPass, int userId)
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.Admins.FirstOrDefault(c => c.UserId == userId);
                if (info == null) return false;
                return info.PassWord == Encryption.SHA256Encrypt(Encryption.MD5(oldPass));
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPass"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool UpdatePass(string newPass, int userId)
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.Admins.FirstOrDefault(c => c.UserId == userId);
                if (info == null) return false;
                info.PassWord = Encryption.SHA256Encrypt(Encryption.MD5(newPass));
                db.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 是否已存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static bool checkUserNameExists(string userName)
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.Admins.FirstOrDefault(c => c.UserName == userName && c.IsDelete == false);
                if (info == null)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool DeleteAdmin(int userId)
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.Admins.FirstOrDefault(c => c.UserId == userId);
                if (info == null) return false;
                info.IsDelete = true;
                return db.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 启用/禁用
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="enable"></param>
        /// <returns></returns>
        public static bool EnableAdmin(int userId, bool enable)
        {
            using (var db = new DavidDBEntities())
            {
                var info = db.Admins.FirstOrDefault(c => c.UserId == userId);
                if (info == null) return false;
                info.IsEnable = enable;
                return db.SaveChanges() > 0;
            }
        }
    }
}
